﻿package com.mui.display
{
    import com.gengine.global.*;
    import com.mui.utils.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;

    public class ScaleBitmap extends Bitmap implements IDispose
    {
        protected var _isCleared:Boolean = true;
        protected var _isOnStage:Boolean = false;
        protected var _targetUrl:String = "";
        protected var _originalBitmap:BitmapData;
        protected var _createBitmap:BitmapData;
        protected var _scale9Grid:Rectangle = null;
        protected var _keepTexture:Boolean;
        protected var _isDisposed:Boolean = true;
        protected var _width:Number = -1;
        protected var _height:Number = -1;
        private static var defaultBitmapData:BitmapData;

        public function ScaleBitmap(param1:BitmapData = null, param2:String = "auto", param3:Boolean = false, param4:Boolean = false)
        {
            this._originalBitmap = param1;
            super(this._originalBitmap, param2, param3);
            this._keepTexture = param4;
            this.addEventListener(Event.ADDED_TO_STAGE, this.onAddedToStage);
            this.addEventListener(Event.REMOVED_FROM_STAGE, this.onRemoveFromStage);
            return;
        }// end function

        public function clear() : void
        {
            if (this._createBitmap)
            {
                this._isCleared = true;
                this._createBitmap.dispose();
                this._createBitmap = null;
            }
            return;
        }// end function

        public function get targetUrl() : String
        {
            return this._targetUrl;
        }// end function

        public function set targetUrl(param1:String) : void
        {
            this._targetUrl = param1;
            return;
        }// end function

        public function restore() : void
        {
            var _loc_1:Number = NaN;
            var _loc_2:Number = NaN;
            if (this._isCleared)
            {
                _loc_1 = this.scaleX;
                _loc_2 = this.scaleY;
                this.scaleX = 1;
                this.scaleY = 1;
                this.setSize(this._width, this._height);
                this.scaleX = _loc_1;
                this.scaleY = _loc_2;
            }
            return;
        }// end function

        public function createDisposeChildren() : void
        {
            if (this._isDisposed)
            {
                this._isDisposed = false;
            }
            return;
        }// end function

        private function onAddedToStage(event:Event) : void
        {
            this._isOnStage = true;
            this.restore();
            return;
        }// end function

        private function onRemoveFromStage(event:Event) : void
        {
            this._isOnStage = false;
            this.clear();
            return;
        }// end function

        override public function set bitmapData(param1:BitmapData) : void
        {
            this._originalBitmap = param1;
            this._width = param1.width;
            this._height = param1.height;
            if (this._scale9Grid != null)
            {
                if (!this.validGrid(this._scale9Grid))
                {
                    this._scale9Grid = null;
                }
                this.setSize(param1.width, param1.height);
            }
            else
            {
                this.assignBitmapData(this._originalBitmap);
            }
            return;
        }// end function

        override public function set width(param1:Number) : void
        {
            if (param1 != width)
            {
                this.setSize(param1, height);
            }
            return;
        }// end function

        override public function set height(param1:Number) : void
        {
            if (param1 != height)
            {
                this.setSize(width, param1);
            }
            return;
        }// end function

        public function resetBitmapDataAndScale9Grid(param1:BitmapData, param2:Rectangle) : void
        {
            this._scale9Grid = null;
            this._originalBitmap = param1;
            this.scale9Grid = param2;
            param1 = param1;
            return;
        }// end function

        override public function set scale9Grid(param1:Rectangle) : void
        {
            var _loc_2:Number = NaN;
            var _loc_3:Number = NaN;
            if (this._scale9Grid == null && param1 != null || this._scale9Grid != null && !this._scale9Grid.equals(param1))
            {
                if (param1 == null)
                {
                    _loc_2 = width;
                    _loc_3 = height;
                    this._scale9Grid = null;
                    this.assignBitmapData(this._originalBitmap);
                    this.setSize(_loc_2, _loc_3);
                }
                else
                {
                    if (!this.validGrid(param1))
                    {
                        throw new Error("#001 - The _scale9Grid does not match the original BitmapData");
                    }
                    this._scale9Grid = param1.clone();
                    this.resizeBitmap(width, height);
                    scaleX = 1;
                    scaleY = 1;
                }
            }
            return;
        }// end function

        protected function assignBitmapData(param1:BitmapData) : void
        {
            super.bitmapData = param1;
            return;
        }// end function

        private function validGrid(param1:Rectangle) : Boolean
        {
            return param1.right <= this._originalBitmap.width && param1.bottom <= this._originalBitmap.height;
        }// end function

        override public function get scale9Grid() : Rectangle
        {
            return this._scale9Grid;
        }// end function

        public function setSize(param1:Number, param2:Number) : void
        {
            if (this._scale9Grid == null)
            {
                super.width = param1;
                super.height = param2;
            }
            else
            {
                if (this._originalBitmap)
                {
                    param1 = Math.max(param1, this._originalBitmap.width - this._scale9Grid.width);
                    param2 = Math.max(param2, this._originalBitmap.height - this._scale9Grid.height);
                }
                this.resizeBitmap(param1, param2);
            }
            this._width = param1;
            this._height = param2;
            return;
        }// end function

        public function getOriginalBitmapData() : BitmapData
        {
            return this._originalBitmap;
        }// end function

        protected function resizeBitmap(param1:int, param2:int) : void
        {
            var origin:Rectangle;
            var draw:Rectangle;
            var cx:int;
            var cy:int;
            var shape:Shape;
            var drawBitmapData:BitmapData;
            var drawBitmapDataResized:BitmapData;
            var w:* = param1;
            var h:* = param2;
            if (!(w >= 1 && h >= 1))
            {
                return;
            }
            if (w > 2000 || h > 2000)
            {
                return;
            }
            if (!this._isOnStage)
            {
                if (!defaultBitmapData)
                {
                    defaultBitmapData = new BitmapData(1, 1);
                }
                this.assignBitmapData(defaultBitmapData);
            }
            if (this._createBitmap)
            {
                this._createBitmap.dispose();
            }
            try
            {
                this._createBitmap = new BitmapData(w, h, true, 0);
            }
            catch (ee:Error)
            {
                if (Global.isDebugModle)
                {
                    throw new Error("w:" + w + ",h:" + h + "\n" + ee.message + ";" + ee.getStackTrace());
                }
                else
                {
                    return;
                }
            }
            this._isCleared = false;
            var rows:Array;
            var cols:Array;
            var dRows:Array;
            var dCols:Array;
            var mat:* = new Matrix();
            if (!this._keepTexture)
            {
                cx;
                while (cx < 3)
                {
                    
                    cy;
                    while (cy < 3)
                    {
                        
                        origin = new Rectangle(cols[cx], rows[cy], cols[(cx + 1)] - cols[cx], rows[(cy + 1)] - rows[cy]);
                        draw = new Rectangle(dCols[cx], dRows[cy], dCols[(cx + 1)] - dCols[cx], dRows[(cy + 1)] - dRows[cy]);
                        mat.identity();
                        mat.a = draw.width / origin.width;
                        mat.d = draw.height / origin.height;
                        mat.tx = draw.x - origin.x * mat.a;
                        mat.ty = draw.y - origin.y * mat.d;
                        this._createBitmap.draw(this._originalBitmap, mat, null, null, draw, smoothing);
                        cy = (cy + 1);
                    }
                    cx = (cx + 1);
                }
                this.assignBitmapData(this._createBitmap);
            }
            else
            {
                shape = new Shape();
                cx;
                while (cx < 3)
                {
                    
                    cy;
                    while (cy < 3)
                    {
                        
                        draw = new Rectangle(cols[cx], rows[cy], cols[(cx + 1)] - cols[cx], rows[(cy + 1)] - rows[cy]);
                        drawBitmapData = new BitmapData(cols[(cx + 1)] - cols[cx], rows[(cy + 1)] - rows[cy], true, 0);
                        drawBitmapData.copyPixels(this._originalBitmap, draw, new Point(0, 0));
                        shape.graphics.clear();
                        shape.graphics.beginBitmapFill(drawBitmapData);
                        shape.graphics.drawRect(0, 0, dCols[(cx + 1)] - dCols[cx], dRows[(cy + 1)] - dRows[cy]);
                        drawBitmapDataResized = new BitmapData(dCols[(cx + 1)] - dCols[cx], dRows[(cy + 1)] - dRows[cy], true, 0);
                        draw = new Rectangle(0, 0, dCols[(cx + 1)] - dCols[cx], dRows[(cy + 1)] - dRows[cy]);
                        drawBitmapDataResized.draw(shape);
                        this._createBitmap.copyPixels(drawBitmapDataResized, draw, new Point(dCols[cx], dRows[cy]));
                        drawBitmapData.dispose();
                        drawBitmapData;
                        drawBitmapDataResized.dispose();
                        cy = (cy + 1);
                    }
                    cx = (cx + 1);
                }
                this.assignBitmapData(this._createBitmap);
            }
            return;
        }// end function

        public function dispose(param1:Boolean = true) : void
        {
            if (this._isDisposed)
            {
                return;
            }
            if (this.parent)
            {
                this.parent.removeChild(this);
            }
            this.clear();
            super.bitmapData = null;
            this._isDisposed = true;
            this._scale9Grid = null;
            this._isCleared = true;
            this._isOnStage = false;
            this._targetUrl = "";
            super.x = 0;
            super.y = 0;
            if (param1)
            {
                UICompomentPool.disposeUICompoment(this);
            }
            return;
        }// end function

    }
}
